//
//  rootTabBarViewController.swift
//  ledian
//
//  Created by 三秒文化 on 16/2/27.
//  Copyright © 2016年 51ledian. All rights reserved.
//

import UIKit
import RDVTabBarController

enum userType:String {
    
    case Finder = "finder"
    case Boss = "boss"
}

class LDMainTabBarVC: RDVTabBarController{
    
    internal var clientType = DDClientType.Cattle {
        
        didSet{
            
            setUp()
            setupData()
        }
    }
    internal var typeName = String()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        let diviLine = UIView()
//        diviLine.backgroundColor = LDLineColorLightGray
//        tabBar.addSubview(diviLine)
//        
//        tabBar.snp_makeConstraints { (make) in
//            make.top.equalTo(tabBar.snp_top)
//            make.height.equalTo(1)
//            make.width.equalTo(375)
//        }

    }
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
         setTabbarAppearance()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setUp() {
        

        let vc1:UIViewController!
        let vc2:UIViewController!
        let vc3:UIViewController!
        let vc4:UIViewController!
        
        
        if clientType == DDClientType.Cattle {
            
            vc1 = DDDiscoverViewController()
            vc2 = LDWorthVC()
            vc3 = DDMessageListViewController()
//            vc3 = LDFinderListMessageVC()
            vc4 = LDFinderMyVC()
            
        } else {
            
            vc1 = DDBossDiscoverViewController()
            vc2 = LDWorthVC()
            vc3 = DDBossMessageViewController()
//            vc3 = LDFinderListMessageVC()
            vc4 = LDBossMeVC()
        }
        
        
        let discover = LDRootNavVC(rootViewController:vc1)
        let worth = LDRootNavVC(rootViewController:vc2)//LDCustomRootNavVC(rootViewController:vc2)
        let message = LDRootNavVC(rootViewController:vc3)
        let me =  LDRTRootNavVC(rootViewController:vc4)

        
        viewControllers = [discover,worth,message,me]
        tabBar.setHeight(49)
        
        let tarbarnor = ["发现_normal","价值_normal","消息_normal","我的_normal"]
        let tarbarsel = ["发现_select","价值_select","消息_select","我的_select"]
        let titleArr = ["发现","D＋","消息","我的"]
        
        let baceimg = UIImage(named: "tabbar_backimg")
    
        for index in 0..<tabBar.items.count {
            
            let rdvitem = tabBar.items[index] as? RDVTabBarItem
            
            let norimg = UIImage(named: tarbarnor[index])
            let selimg = UIImage(named: tarbarsel[index])
            
            rdvitem?.setFinishedSelectedImage(norimg, withFinishedUnselectedImage: selimg)
            rdvitem?.title = titleArr[index]
            

            rdvitem?.selectedTitleAttributes = [NSForegroundColorAttributeName:LDGolbalColor.Green,
                                                NSFontAttributeName:UIFont.systemFontOfSize(10)]
            rdvitem!.unselectedTitleAttributes = [NSForegroundColorAttributeName:LDFontColor.DeepGray,
                                                  NSFontAttributeName:UIFont.systemFontOfSize(10)]
            rdvitem?.backgroundColor = LDGolbalColor.White
            rdvitem?.titlePositionAdjustment = UIOffsetMake(0, 4)
            rdvitem?.imagePositionAdjustment = UIOffsetMake(0, 1)
            
            rdvitem?.setBackgroundSelectedImage(baceimg, withUnselectedImage: baceimg)
            
        }
        
//        creatTarBar(titles:"发现", norImg: "发现_select", secImg: "发现_normal", viewController: vc1, tabBarTag: 100)
//        creatTarBar(titles:"价值", norImg: "价值_select", secImg: "价值_normal", viewController: vc2, tabBarTag: 101)
//        creatTarBar(titles:"消息", norImg: "消息_select", secImg: "消息_normal", viewController: vc3, tabBarTag: 102)
//        creatTarBar(titles:"我的", norImg: "我的_select", secImg: "我的_normal", viewController: vc4, tabBarTag: 103)
        
    }
    //初始化用户信息
    func setupData() {
        
        if clientType == DDClientType.Cattle {
            
            LDUserInfo.sharedInstance
        } else if clientType == DDClientType.Boss {
            
            LDBossInfo.sharedInstance
        }
    }
    
//    func creatTarBar( titles titles:NSString,norImg:NSString,secImg:NSString,viewController:UIViewController?,tabBarTag:Int){
//        
//        let nav:LDRootNavVC = LDRootNavVC(rootViewController:viewController!)
//        
//        viewController!.title = titles as String
//        nav.tabBarItem.image = UIImage(named: norImg as String)
//        
//        let oriNorImg = UIImage(named: norImg as String)
//        nav.tabBarItem.image = oriNorImg?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
//        
//        let oriSecImg = UIImage(named: secImg as String)
//        nav.tabBarItem.selectedImage = oriSecImg?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
//        
//        nav.tabBarItem.tag = tabBarTag
//        
//        nav.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:LDGolbalColor.Green], forState: UIControlState.Selected)
//        nav.view.backgroundColor = LDGolbalColor.BgGoundGray
//        self.addChildViewController(nav)
//    }
//    
    private func setTabbarAppearance(){
        
        
        UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
//        let tabBarAppearances = UITabBar.appearance()
//        let rect = CGRectMake(0, 0, LDFrame.ScreenWidth, 49)
//        tabBarAppearances.backgroundImage = UIImage.setImageWithColor(UIColor.whiteColor(),frame: rect)
//        tabBarAppearances.opaque = false
//        tabBarAppearances.tintColor = LDGolbalColor.Green
        
    }
}
